package com.security.frame.token.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

/**
 * todo
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/11/29
 */
@Configuration
public class TokenStoreConfig {

     @Autowired
     private RedisConnectionFactory redisConnectionFactory;

     @Bean
     @ConditionalOnProperty(prefix = "frame.security.common",name = "storeType",havingValue = "redis")
     public TokenStore redisTokenStore(){
         return new RedisTokenStore(redisConnectionFactory);
     }

     @Configuration
     @ConditionalOnProperty(prefix = "frame.security.common",name = "storeType",havingValue = "jwt",matchIfMissing = true)
     public static class jwtTokenConfig{
          @Bean
          public TokenStore jwtTokenStore(){
               return new JwtTokenStore(jwtAccessTokenConverter());
          }

          @Bean
          public JwtAccessTokenConverter jwtAccessTokenConverter(){
               JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
               accessTokenConverter.setSigningKey("密钥 ");
               return accessTokenConverter;
          }

          @Bean
          @ConditionalOnMissingBean(name = "jwtTokenEnhancer")
          public TokenEnhancer jwtTokenEnhancer(){
               return new FrameJwtTokenEnhancer();
          }
     }

}
